import flash.events.Event;
import flash.filesystem.*;

import mx.controls.*;

private function npProjectBrowseClick():void {
	var myFile:File = new File();
	 
	myFile.addEventListener(Event.SELECT, npProjectFile);
	myFile.browseForSave("Choose project file name");
}

private function npLocaleBrowseClick():void {
	var myFile:File = new File();
	 
	myFile.addEventListener(Event.SELECT, npLocaleFile);
	myFile.browseForSave("Choose locale file name");
}

private function npProjectFile(event:Event):void 
{
    var newFile:File = event.target as File;
    
    if (newFile.extension != "flextrans") {
    	newFile = new File(newFile.nativePath + ".flextrans");
    }
    
    if (!newFile.exists)
    {
       npProjectFilename.text = newFile.nativePath;
    }
    else {
    	Alert.show("File already exists, choose a different name.");
    }
    projectFilename = newFile.nativePath;
}

private function npLocaleFile(event:Event):void 
{
    var newFile:File = event.target as File;
    
    if (newFile.extension != "properties") {
    	newFile = new File(newFile.nativePath + ".properties");
    }
    
    if (newFile.exists) {
    	Alert.show("File already exists and will be overwritten when generated.");
    }
    
    npLocaleFilename.text = newFile.nativePath;
}

private function createNewProject():void {
	var str:String;
	var newFile:File = new File(projectFilename);
	var stream:FileStream = new FileStream();
	
	masterLocale = npLocaleCombo.value.toString();
	
    project = 	<ftproject>
    				<properties masterlocale=""/>
    				<locale code="" file="" />
    			</ftproject>;
    
    project.properties[0].@masterlocale = masterLocale;
    project.locale[0].@code = masterLocale;
    project.locale[0].@file = npLocaleFilename.text;
    str = project.toXMLString();
    
    stream.open(newFile, FileMode.WRITE);
    stream.writeUTFBytes(str);
    stream.close();
    
    currentState = "openProject";
}
